
package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.entity.Cabinet;
import com.example.train_operate.entity.Inspection;
import com.example.train_operate.mapper.DailyInspectionJudgmentMapper;
import com.example.train_operate.service.DailyInspectionJudgmentService;
import com.example.train_operate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

@Service
public class DailyInspectionJudgmentServiceImpl extends ServiceImpl<DailyInspectionJudgmentMapper, Inspection> implements DailyInspectionJudgmentService {
        @Autowired
        private DailyInspectionJudgmentMapper instructionMapper;


        @Override
        public Result addDailyInspectionJudgment(Inspection inspection) {
                try {
                        QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("step", inspection.getStep());
                        if (instructionMapper.findByStep(inspection.getStep()) != null) {
                                return Result.fail().message("添加失败！");
                        }
                        String photoUrl = inspection.getPhotoUrl();
                        if(photoUrl != null && !photoUrl.isEmpty()){
                                String substring = photoUrl.substring(FILE_URL.length());
                                inspection.setPhotoUrl(substring);
                        }
                        instructionMapper.addByStep(inspection);
                        return Result.ok().message("添加成功");
                }catch (Exception e)
                {
                        return Result.fail().message("添加失败！");
                }
        }

        @Override
        public Result updateDailyInspectionJudgment(Inspection inspection) {
                if(instructionMapper.findByStep(inspection.getStep())==null){
                        Result.ok(instructionMapper.addByStep(inspection)).message("修改成功");
                }
                return Result.ok(instructionMapper.updateByStep(inspection)).message("修改成功");
        }
        @Override
        public Result deleteDailyInspectionJudgment(Integer step) {
                try {
                        QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("step", step);
                        Inspection inspection = instructionMapper.findByStep(step);
                        if (inspection == null) {
                                return Result.fail().message("删除失败！");
                        }
                        instructionMapper.deleteByStep(step);
                        return Result.ok().message("删除成功");
                }catch (Exception e)
                {
                        return Result.fail().message("删除失败！");
                }
        }

        @Override
        public Result getDailyInspectionJudgmentByStep(Integer step) {
                try {
                        QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("step", step);
                        Inspection inspection = instructionMapper.findByStep(step);
                        if (inspection == null) {
                                return Result.fail().message("查询失败！");
                        }
                        String photoUrl = inspection.getPhotoUrl();
                        String[] photoUrlList = null;
                        if (photoUrl != null && !photoUrl.isEmpty()){
                                photoUrlList = photoUrl.split(",");
                        }
                        inspection.setPhotoUrlList(photoUrlList);
                        return Result.ok(inspection);
                }catch (Exception e)
                {
                        return Result.fail().message("查询失败！");
                }
        }
        @Override
        public Result ListDailyInspectionJudgment() {
                try{
                        List<Inspection> inspectionList = instructionMapper.findAll();
                        inspectionList.forEach(inspection -> {
                                String photoUrl = inspection.getPhotoUrl();
                                if (photoUrl != null && !photoUrl.isEmpty()){
                                        String[] photoUrlList = photoUrl.split(",");
                                        inspection.setPhotoUrlList(photoUrlList);
                                }
                        });
                        return Result.ok(inspectionList);
                } catch (Exception e){
                        return Result.fail().message("查询失败！");
                }
        }

}